package edu.isquen.core.base.action;

import java.io.Serializable;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;

import javax.servlet.http.HttpServletRequest;

/****************************************************
* 文件名：ParamsTable.java
* 文件描述：参数类
* 创建人： isquen, 2011 年6 月10 日
* 版本号：1.0
* 修改记录：
****************************************************/
public class ParamsTable implements Serializable {

	private static final long serialVersionUID = 4077191001144574996L;
	
	private HashMap<String, Object> params =new HashMap<String, Object>();
	
	private String siteHttp;
	
	private String contextPath;
	
	private String sessionid;
	
	private transient HttpServletRequest httpRequest;
	
	public ParamsTable() {}
	
	public ParamsTable(ParamsTable params) {
		this.setHttpRequest(params.getHttpRequest());
		this.setSessionid(params.getSessionid());
		this.setContextPath(params.getContextPath());
	}
	
	@SuppressWarnings("deprecation")
	public static ParamsTable convertHTTP(HttpServletRequest request) {
		ParamsTable params = new ParamsTable();

		request.getSession().getServletContext();
		params.setSiteHttp(request.getServerName());
		params.contextPath = request.getContextPath();
		params.setParameter("realPath", request.getRealPath("/"));

		Enumeration<?> en = request.getParameterNames();
		if (en != null) {
			while (en.hasMoreElements()) {
				String name = (String) en.nextElement();
				String[] list = request.getParameterValues(name);

				if (list != null && list.length > 1)
					params.setParameter(name, list);
				else
					params.setParameter(name, request.getParameter(name));
			}
		}

		params.sessionid = request.getSession().getId();

		params.setHttpRequest(request);
		return params;
	}
	
	public void setParameter(String key,Object value){
		params.put(key, value);
	}
	
	public void removeParameter(String name) {
		params.remove(name);
	}
	
	public Object getParameter(String key){
		return params.get(key);
	}
	
	public String getParameterAsString(String key){
		Object obj = params.get(key);
		return (obj != null) ? obj.toString() : null;
	}
	
	
	public Iterator<String> getParameterNames() {
		return params.keySet().iterator();
	}


	public HttpServletRequest getHttpRequest() {
		return httpRequest;
	}

	public void setHttpRequest(HttpServletRequest httpRequest) {
		this.httpRequest = httpRequest;
	}

	public String getContextPath() {
		return contextPath;
	}

	public void setContextPath(String contextPath) {
		this.contextPath = contextPath;
	}

	public String getSessionid() {
		return sessionid;
	}

	public void setSessionid(String sessionid) {
		this.sessionid = sessionid;
	}

	public void setSiteHttp(String siteHttp) {
		this.siteHttp = siteHttp;
	}

	public String getSiteHttp() {
		return siteHttp;
	}

	
}
